package com.feidee.bigdata.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:CHQ
 * @Date:2020/5/9 16:54
 * @Description
 */
@RestController
public class ProviderController {
    @Value("${service.port}")
    String port;

    @RequestMapping("testSentinel/{id}")
    @SentinelResource(value = "testSentinel", blockHandler = "dealTestSentinel", fallback = "globalHandel",
            exceptionsToIgnore =RuntimeException.class)
    public String testSentinel(@PathVariable String id) {
        if ("2".equals(id)) {
            throw new RuntimeException("非法id");
        }
        return "hi i am sentinel ,i am from " + " " + port;
    }

    //针对sentinel配置页面的限流降级配置所触发
    //当两者都配置时，此方法优先执行
    public String dealTestSentinel(@PathVariable String id,BlockException execption) {
        return "开始限流 " + execption.getMessage();
    }

    //针对java代码运行时异常触发
    public String globalHandel(@PathVariable String id) {
        return "兜底了";
    }

    @RequestMapping("test")
    public String test(){
        return  "hi i am testSentinel";
    }
}
